﻿
#if !( defined(_PSVITA) || defined(_XBOXONE) )

//----------------------------------------------------------------------------------
// Include
//----------------------------------------------------------------------------------
#if defined(_WIN32) && !defined(_PS4) 
#include <winsock2.h>
#pragma comment( lib, "ws2_32.lib" )
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif

#include "Effekseer.Socket.h"

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
namespace Effekseer {
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Socket::Initialize()
{
#if defined(_WIN32) && !defined(_PS4) 
	// Initialize  Winsock
	WSADATA m_WsaData;
	::WSAStartup( MAKEWORD(2,0), &m_WsaData );
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Socket::Finalize()
{
#if defined(_WIN32) && !defined(_PS4) 
	// Dispose winsock or decrease a counter
	WSACleanup();
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
EfkSocket Socket::GenSocket()
{
	return ::socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Socket::Close( EfkSocket s )
{
#if defined(_WIN32) && !defined(_PS4) 
	::closesocket( s );
#else
	::close( s );
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void Socket::Shutsown( EfkSocket s )
{
#if defined(_WIN32) && !defined(_PS4) 
	::shutdown( s, SD_BOTH );
#else
	::shutdown( s, SHUT_RDWR );
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
bool Socket::Listen( EfkSocket s, int32_t backlog )
{
#if defined(_WIN32) && !defined(_PS4) 
	return ::listen( s, backlog ) != SocketError;
#else
	return listen( s, backlog ) >= 0;
#endif
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
 } 
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------

#endif	// #if !( defined(_PSVITA) || defined(_XBOXONE) )

